package com.tvsinfo.jdk.demo.case7_parallel;

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * 不同集合对象的可分解性
 */
public class Parallel2_resolve {

    public static void main(String[] args) {

        System.out.println("--------------------------");
        Parallel2_resolve parallel2_split = new Parallel2_resolve();

        List list1 = parallel2_split.getList(ArrayList::new);
        List list2 = parallel2_split.getList(LinkedList::new);

        for (int i = 0; i < 10; i++) {
            parallel2_split.test(list1);
            parallel2_split.test(list1);
            parallel2_split.test(list1);
            parallel2_split.test(list1);
            System.out.println("--------------------------");
            parallel2_split.test(list2);
            parallel2_split.test(list2);
            parallel2_split.test(list2);
            parallel2_split.test(list2);
            System.out.println("--------------------------");
        }
    }


    void test(List<Parallel1_Stream.ParallelCase> numbers) {
        System.out.println(numbers.getClass() + "++++++++++++");
        Long startTimes = System.currentTimeMillis();
        List<Integer> collect = numbers.stream().parallel().map(Parallel1_Stream.ParallelCase::getDoCondition).limit(100).collect(Collectors.toList());
        Long endTimes = System.currentTimeMillis();
        System.out.println("parallel-> startTime：" + startTimes + " end:" + endTimes + " 总耗时：" + (endTimes - startTimes));
    }

    public List<Parallel1_Stream.ParallelCase> getList(Supplier<List<Parallel1_Stream.ParallelCase>> supplier) {
        List<Parallel1_Stream.ParallelCase> numbers = supplier.get();
        Random random = new Random();
        Long startTimes = System.currentTimeMillis();
        for (int i = 0; i < 20000000; i++) {
            numbers.add(new Parallel1_Stream.ParallelCase(Integer.valueOf(random.nextInt(100000))));
        }
        Long endTimes = System.currentTimeMillis();
        System.out.println("createList-> ：" + numbers.getClass() + " 总耗时：" + (endTimes - startTimes));
        return numbers;
    }
}

